/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.commands;

import controller.BidirectionalHashMap;
import core.ConnectException;
import core.Node;
import core.TriggerPort;
import javax.swing.JOptionPane;
import model.graphic.objects.GTrigger;
import model.graphic.objects.GTriggerPort;
import model.graphic.objects.RectangleGraphical;
import view.GUI.LabelReader;

/**
 *
 * @author Jelle, modified by Ben
 */
public class ExecuteCommand extends BasicCommand {

    private GTrigger trigger;

    public ExecuteCommand(GTrigger trigger) {
        this.trigger = trigger;
    }

    public TriggerPort getTriggerPort(){
        RectangleGraphical gn = (RectangleGraphical) ((GTriggerPort) trigger.getBase()).getBase();
        Node n = (Node) BidirectionalHashMap.getInstance().get(gn);
        return (TriggerPort)n.getPort(trigger.getBase().getName());
    }

    @Override
    public void execute() {
        controller.getOutput().append(LabelReader.getInstance().getString("RunningProgram"));
        try {
            TriggerPort p = getTriggerPort();
            ExecuteThread et = new ExecuteThread( p, trigger, gview);
            gview.setExecuteThread(et);
            et.start();
        } catch (ConnectException ex) {
            showPopup("ExcecuteCommand: " + ex.getMessage(), LabelReader.getInstance().getString("Error"), JOptionPane.ERROR_MESSAGE);
        }
        controller.getDebug().append(LabelReader.getInstance().getString("YourProgramIsNowBeeingExecuted"));
        gview.repaint();
    }
}
